package org.linhadig.swing.util;

import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

@SuppressWarnings("serial")
public class MaxLengthTextDocument extends PlainDocument {
	private int maxChars;

	public MaxLengthTextDocument() {
	}

	public MaxLengthTextDocument(int maxChars) {
		this.maxChars = maxChars;
	}

	@Override
	public void insertString(int offs, String str, AttributeSet a)
			throws BadLocationException {
		
		if (str != null && (getLength() + str.length() < maxChars)) {
			super.insertString(offs, str, a);
		}
	}

	public int getMaxChars() {
		return maxChars;
	}

	public void setMaxChars(int maxChars) {
		this.maxChars = maxChars;
	}

}